package com.wu.family.utils;

import com.idle.view.ProgressAroundView;
import com.wu.family.R;
import com.wu.family.config.Constant;
import com.wu.family.model.AudioPhoto;
import com.wu.family.utils.audio.SoundMgr;
import com.wu.family.utils.audio.SoundPlayer;
import com.wu.family.utils.audio.SoundMgr.SoundCallBack;
import com.wu.family.utils.audio.SoundPlayer.OnCompleteItf;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;


public class ProgressAroundViewUtil {
	public static void config(final Context context, final ProgressAroundView progArdView, 
			final AudioPhoto audioPhoto, final int position ) {
		progArdView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (audioPhoto.isPlaying()) {
					progArdView.stopTiming();
					progArdView.setProgress(0);
					SoundPlayer.getInstance().stop();
					audioPhoto.setPlaying(false);
					progArdView.setImageResource(R.drawable.feed_btn_audioplay);
				}else {
					SoundMgr.getInstance().getSoundPath(audioPhoto.getAudioUrl(), new SoundCallBack(){
						@Override
						public void onFoundCachePath(String url, String path) {
							SoundPlayer.getInstance().playMusic(path, new OnCompleteItf() {
								@Override
								public void OnPreStart(int duration) {
									audioPhoto.setPlaying(true);
									progArdView.setImageResource(R.drawable.feed_btn_audiopause);
									Intent intent = new Intent(Constant.Action.START_SOUND_BROADCAST);
									intent.putExtra("AudioPhoto", audioPhoto);
									intent.putExtra("position", position); 
									context.sendBroadcast(intent);
									
									progArdView.setTotalTime(duration!=-1?duration:audioPhoto.getAudioTime()*1000);
									progArdView.startTiming();
								}
								@Override
								public void OnComplete() {
									progArdView.setProgress(0);
									progArdView.stopTiming();
									progArdView.setImageResource(R.drawable.feed_btn_audioplay);
									audioPhoto.setPlaying(false);
									//notifyDataSetChanged();
								}

								@Override
								public void OnUnComplete() {
									progArdView.setProgress(0);
									progArdView.stopTiming();
									progArdView.setImageResource(R.drawable.feed_btn_audioplay);
									audioPhoto.setPlaying(false);
									//notifyDataSetChanged();
								}
							});
						}});
				}
			}
		});
	}
}
